<!--
 * @Descripttion: (批次号工位绑定表/BatchBinding)
 * @Author: (admin)
 * @Date: (2024-10-21)
-->
<template>
  <div>
    <el-form :model="queryParams" label-position="right" inline ref="queryRef" v-show="showSearch" @submit.prevent>
      <el-form-item>
        <el-button icon="search" type="primary" @click="handleQuery">{{ $t('btn.search') }}</el-button>
        <el-button icon="refresh" @click="resetQuery">{{ $t('btn.reset') }}</el-button>
      </el-form-item>
    </el-form>
    <!-- 工具区域 -->
    <el-row :gutter="15" class="mb10">
      <el-col :span="1.5">
        <el-button type="primary" v-hasPermi="['batchbinding:add']" plain icon="plus" @click="handleAdd">
          {{ $t('btn.add') }}
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList" :columns="columns"></right-toolbar>
    </el-row>

    <el-table
      :data="dataList"
      v-loading="loading"
      ref="table"
      border
      header-cell-class-name="el-table-header-cell"
      highlight-current-row
      @sort-change="sortChange"
      >
      <el-table-column prop="id" label="序号" align="center" v-if="columns.showColumn('id')"/>
      <el-table-column prop="lineCode" label="产线代号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('lineCode')"/>
      <el-table-column prop="stationCode" label="工位代号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('stationCode')"/>
      <el-table-column prop="batchCode" label="批次条码" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('batchCode')"/>
      <el-table-column prop="partCode" label="零件代号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('partCode')"/>
      <el-table-column prop="supplierCode" label="供应商代码" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('supplierCode')"/>
      <el-table-column prop="materialCode" label="物料代码" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('materialCode')"/>
      <el-table-column prop="singleQty" label="批次数量" align="center" v-if="columns.showColumn('singleQty')"/>
      <el-table-column prop="partUniCode" label="统一代号" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('partUniCode')"/>
      <el-table-column prop="updateTime" label="更新时间" :show-overflow-tooltip="true"  v-if="columns.showColumn('updateTime')"/>
      <el-table-column prop="creatorName" label="创建人" align="center" :show-overflow-tooltip="true" v-if="columns.showColumn('creatorName')"/>
      <el-table-column label="操作" width="160">
        <template #default="scope">
          <el-button type="success" size="small" icon="edit" title="编辑" v-hasPermi="['batchbinding:edit']" @click="handleUpdate(scope.row)"></el-button>
          <el-button type="danger" size="small" icon="delete" title="删除" v-hasPermi="['batchbinding:delete']" @click="handleDelete(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" @pagination="getList" />


    <!-- 添加或修改批次号工位绑定表对话框 -->
    <el-dialog :title="title" :lock-scroll="false" v-model="open" >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-row :gutter="20">
            
          <el-col :lg="12" v-if="opertype != 1">
            <el-form-item label="序号" prop="id">
              <el-input-number v-model.number="form.id" controls-position="right" placeholder="请输入序号" :disabled="true"/>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="产线代号" prop="lineCode">
              <el-input v-model="form.lineCode" placeholder="请输入产线代号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="工位代号" prop="stationCode">
              <el-input v-model="form.stationCode" placeholder="请输入工位代号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="批次条码" prop="batchCode">
              <el-input v-model="form.batchCode" placeholder="请输入批次条码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="零件代号" prop="partCode">
              <el-input v-model="form.partCode" placeholder="请输入零件代号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="供应商代码" prop="supplierCode">
              <el-input v-model="form.supplierCode" placeholder="请输入供应商代码" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="物料代码" prop="materialCode">
              <el-input v-model="form.materialCode" placeholder="请输入物料代码" />
            </el-form-item>
          </el-col>
            
          <el-col :lg="12">
            <el-form-item label="批次数量" prop="singleQty">
              <el-input v-model.number="form.singleQty" placeholder="请输入批次数量" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="统一代号" prop="partUniCode">
              <el-input v-model="form.partUniCode" placeholder="请输入统一代号" />
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="更新时间" prop="updateTime">
              <el-date-picker v-model="form.updateTime" type="datetime" :teleported="false" placeholder="选择日期时间"></el-date-picker>
            </el-form-item>
          </el-col>

          <el-col :lg="12">
            <el-form-item label="创建人" prop="creatorName">
              <el-input v-model="form.creatorName" placeholder="请输入创建人" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer v-if="opertype != 3">
        <el-button text @click="cancel">{{ $t('btn.cancel') }}</el-button>
        <el-button type="primary" @click="submitForm">{{ $t('btn.submit') }}</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="batchbinding">
import { listBatchBinding,
 addBatchBinding, delBatchBinding, 
 updateBatchBinding,getBatchBinding, 
 } 
from '@/api/business/batchbinding.js'
const { proxy } = getCurrentInstance()
const ids = ref([])
const loading = ref(false)
const showSearch = ref(true)
const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  sort: '',
  sortType: 'asc',
})
const columns = ref([
  { visible: true, prop: 'id', label: '序号' },
  { visible: true, prop: 'lineCode', label: '产线代号' },
  { visible: true, prop: 'stationCode', label: '工位代号' },
  { visible: true, prop: 'batchCode', label: '批次条码' },
  { visible: true, prop: 'partCode', label: '零件代号' },
  { visible: true, prop: 'supplierCode', label: '供应商代码' },
  { visible: true, prop: 'materialCode', label: '物料代码' },
  { visible: true, prop: 'singleQty', label: '批次数量' },
  { visible: false, prop: 'partUniCode', label: '统一代号' },
  { visible: false, prop: 'updateTime', label: '更新时间' },
  { visible: false, prop: 'creatorName', label: '创建人' },
])
const total = ref(0)
const dataList = ref([])
const queryRef = ref()
const defaultTime = ref([new Date(2000, 1, 1, 0, 0, 0), new Date(2000, 2, 1, 23, 59, 59)])


var dictParams = [
]


function getList(){
  loading.value = true
  listBatchBinding(queryParams).then(res => {
    const { code, data } = res
    if (code == 200) {
      dataList.value = data.result
      total.value = data.totalNum
      loading.value = false
    }
  })
}

// 查询
function handleQuery() {
  queryParams.pageNum = 1
  getList()
}

// 重置查询操作
function resetQuery(){
  proxy.resetForm("queryRef")
  handleQuery()
}
// 自定义排序
function sortChange(column) {
  var sort = undefined
  var sortType = undefined

  if (column.prop != null && column.order != null) {
    sort = column.prop
    sortType = column.order

  }
  queryParams.sort = sort
  queryParams.sortType = sortType
  handleQuery()
}

/*************** form操作 ***************/
const formRef = ref()
const title = ref('')
// 操作类型 1、add 2、edit 3、view
const opertype = ref(0)
const open = ref(false)
const state = reactive({
  single: true,
  multiple: true,
  form: {},
  rules: {
  },
  options: {
  }
})

const { form, rules, options, single, multiple } = toRefs(state)

// 关闭dialog
function cancel(){
  open.value = false
  reset()
}

// 重置表单
function reset() {
  form.value = {
    id: null,
    lineCode: null,
    stationCode: null,
    batchCode: null,
    partCode: null,
    supplierCode: null,
    materialCode: null,
    singleQty: null,
    partUniCode: null,
    updateTime: null,
    creatorName: null,
  };
  proxy.resetForm("formRef")
}


// 添加按钮操作
function handleAdd() {
  reset();
  open.value = true
  title.value = '添加批次号工位绑定表'
  opertype.value = 1
}
// 修改按钮操作
function handleUpdate(row) {
  reset()
  const id = row.id || ids.value
  getBatchBinding(id).then((res) => {
    const { code, data } = res
    if (code == 200) {
      open.value = true
      title.value = '修改批次号工位绑定表'
      opertype.value = 2

      form.value = {
        ...data,
      }
    }
  })
}

// 添加&修改 表单提交
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {

      if (form.value.id != undefined && opertype.value === 2) {
        updateBatchBinding(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addBatchBinding(form.value).then((res) => {
            proxy.$modal.msgSuccess("新增成功")
            open.value = false
            getList()
          })
      }
    }
  })
}

// 删除按钮操作
function handleDelete(row) {
  const Ids = row.id || ids.value

  proxy
    .$confirm('是否确认删除参数编号为"' + Ids + '"的数据项？', "警告", {
      confirmButtonText: proxy.$t('common.ok'),
      cancelButtonText: proxy.$t('common.cancel'),
      type: "warning",
    })
    .then(function () {
      return delBatchBinding(Ids)
    })
    .then(() => {
      getList()
      proxy.$modal.msgSuccess("删除成功")
    })
}





handleQuery()
</script>